/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.multishop.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.enums.OfflineHandleEventType;
import com.yami.shop.bean.enums.ProdStatusEnums;
import com.yami.shop.bean.enums.ProdType;
import com.yami.shop.bean.enums.ShopStatus;
import com.yami.shop.bean.event.ProdChangeEvent;
import com.yami.shop.bean.event.ProdChangeStatusEvent;
import com.yami.shop.bean.model.*;
import com.yami.shop.bean.param.OfflineHandleEventAuditParam;
import com.yami.shop.bean.param.ProductExportParam;
import com.yami.shop.bean.param.ProductParam;
import com.yami.shop.bean.param.RetailProdParam;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.i18n.LanguageEnum;
import com.yami.shop.common.util.Json;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.SkuRetailMapper;
import com.yami.shop.groupbuy.comment.api.dto.ApiGroupActivityDto;
import com.yami.shop.groupbuy.comment.enums.ActivityStatusEnum;
import com.yami.shop.groupbuy.comment.model.GroupProd;
import com.yami.shop.groupbuy.comment.service.GroupActivityService;
import com.yami.shop.groupbuy.comment.service.GroupProdService;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * 商品列表、商品发布controller
 *
 * @author lgh
 */
@Controller
@RequestMapping("/prod/prod")
@Api(tags = "商品管理接口", description = "提供商品相关的增删改查接口")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private SkuService skuService;

    @Autowired
    private BasketService basketService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private OfflineHandleEventService offlineHandleEventService;

    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private ProdLangService prodLangService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RetailProdService retailProdService;

    @Autowired
    private SkuRetailMapper skuRetailMapper;

    @Autowired
    private GroupProdService groupProdService;

    @Autowired
    private GroupActivityService groupActivityService;

    /**
     * 分页获取商品信息
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询商品", notes = "根据条件分页查询商品列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "productName", value = "商品名称", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "categoryId", value = "分类ID", paramType = "query", dataType = "long"),
        @ApiImplicitParam(name = "status", value = "商品状态", paramType = "query", dataType = "int")
    })
    public ResponseEntity<IPage<Product>> page(ProductParam product, PageParam<Product> page) {
//        product.setLang(I18nMessage.getDbLang());
        product.setShopId(SecurityUtils.getShopUser().getShopId());
        IPage<Product> products = productService.pageByLang(page,product);
        return ResponseEntity.ok(products);
    }

    /**
     * 分页获取商品信息
     */
    @GetMapping("/recyclePage")
    @ApiOperation(value = "商品回收站列表", notes = "根据条件分页查询商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "productName", value = "商品名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "categoryId", value = "分类ID", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "status", value = "商品状态", paramType = "query", dataType = "int")
    })
    public ResponseEntity<IPage<Product>> recyclePage(ProductParam product, PageParam<Product> page) {
//        product.setLang(I18nMessage.getDbLang());
        product.setShopId(SecurityUtils.getShopUser().getShopId());
        IPage<Product> products = productService.pageByLang(page,product);
        return ResponseEntity.ok(products);
    }

    /**
     * 保存
     */
    @PostMapping("/add")
//    @PreAuthorize("@pms.hasPermission('prod:prod:save')")
    @ApiOperation(value = "添加商品", notes = "添加新商品")
    public ResponseEntity<Void> addProduct(@Valid @RequestBody Product product) {
        return productService.addProduct(product);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新商品", notes = "更新商品信息")
    public ResponseEntity<Void> updateProduct(@Valid @RequestBody Product product) {
        return productService.updateProduct(product);
    }

//    /**
//     * 删除
//     */
//    @DeleteMapping("/{id}")
////    @PreAuthorize("@pms.hasPermission('prod:prod:delete')")
//    @ApiOperation(value = "删除商品", notes = "根据ID删除商品")
//    public ResponseEntity<Void> deleteStateProduct(@PathVariable("id") Long id) {
//        Product product = new Product();
//        product.setProdId(id);
//        product.setStatus(0);
//        productService.updateById(product);
//        return ResponseEntity.ok().build();
//    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
//    @PreAuthorize("@pms.hasPermission('prod:prod:delete')")
    @ApiOperation(value = "永久删除商品", notes = "根据ID删除商品")
    public ResponseEntity<Void> deleteProduct(@PathVariable("id") Long id) {
        return productService.deleteProduct(id);
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/batch")
//    @PreAuthorize("@pms.hasPermission('prod:prod:batchDelete')")
    @ApiOperation(value = "批量删除商品", notes = "根据ID列表批量删除商品")
    public ResponseEntity<Void> batchDeleteProducts(@RequestBody List<Long> ids) {
        return productService.batchDeleteProducts(ids);
    }

//    /**
//     * 分页获取商品信息
//     */
//    @GetMapping("/listProdByIdsAndType")
//    public ResponseEntity<List<Product>> listProdByIdsAndType(ProductParam product) {
//        product.setLang(I18nMessage.getDbLang());
//        product.setShopId(SecurityUtils.getShopUser().getShopId());
//        List<Product> products = productService.listProdByIdsAndType(product);
//        return ResponseEntity.ok(products);
//    }
//
//    /**
//     * 分页获取上架的商品信息
//     */
//    @GetMapping("/pageOnLineProd")
//    public ResponseEntity<IPage<Product>> pageOnLineProd(ProductParam product, PageParam<Product> page) {
//        product.setShopId(SecurityUtils.getShopUser().getShopId());
//        IPage<Product> products = productService.pageProducts(page,product);
//        return ResponseEntity.ok(products);
//    }

//    /**
//     * 获取信息
//     */
//    @GetMapping("/info/{prodId}")
//    @ApiOperation(value = "获取商品详情", notes = "根据商品ID获取商品详细信息")
//    public ResponseEntity<Product> info(@PathVariable("prodId") Long prodId) {
//        Product prod = productService.getProductByProdId(prodId, I18nMessage.getDbLang());
//        if (!Objects.equals(prod.getShopId(), SecurityUtils.getShopUser().getShopId())) {
//            // 查找不到该商品信息
//            throw new YamiShopBindException("yami.product.not.exist");
//        }
//        Brand brand = brandService.getById(prod.getBrandId());
//        if (Objects.nonNull(brand)){
//            prod.setBrand(brand);
//        }
//        List<Sku> skuList = skuService.listByProdId(prodId, I18nMessage.getLang());
//        for (Sku sku : skuList) {
//            for (SkuLang skuLang : sku.getSkuLangList()) {
//                // sku 规格国际化，因为不是关联表的形式，所以多语言一起返回数据
//                if(Objects.equals(skuLang.getLang(),1)){
//                    String propertiesEn = skuLang.getProperties();
//                    String skuName = skuLang.getSkuName();
//                    sku.setPropertiesEn(StrUtil.isNotBlank(propertiesEn)?propertiesEn:sku.getProperties());
//                    sku.setSkuNameEn(StrUtil.isNotBlank(skuName)?skuName:sku.getSkuName());
//                }
//                // 适配一些数据
//                if (StrUtil.isBlank(sku.getPropertiesEn())) {
//                    sku.setPropertiesEn(sku.getProperties());
//                }
//            }
//        }
//        prod.setSkuList(skuList);
//        // 获取语言列表
//        List<ProdLang> prodLangList = prodLangService.list(new LambdaQueryWrapper<ProdLang>().eq(ProdLang::getProdId, prodId));
//        prod.setProdLangList(prodLangList);
//        // 是否是组合商品
//        if (Objects.equals(1,prod.getIsCompose())) {
//            for (Sku sku : skuList) {
//                List<RetailProdParam> skuSingleProds = retailProdService.getBySkuId(sku.getSkuId());
//                if (CollectionUtils.isNotEmpty(skuSingleProds)) {
//                    sku.setSkuSingleProds(skuSingleProds);
//                }
//            }
//        }
//        return ResponseEntity.ok(prod);
//    }



//    /**
//     * 更新商品状态
//     */
//    @PutMapping("/prodStatus")
//    @PreAuthorize("@pms.hasPermission('prod:prod:status')")
//    @ApiOperation(value = "更新商品状态", notes = "更新商品的状态")
//    public ResponseEntity<Void> shopStatus(@RequestBody ProductParam productParam) {
//        Long prodId = productParam.getProdId();
//        Integer prodStatus = productParam.getStatus();
//        Product dbProduct = productService.getProductByProdId(prodId, I18nMessage.getDbLang());
//        if (!Objects.equals(dbProduct.getShopId(), SecurityUtils.getShopUser().getShopId())) {
//            // 查找不到该商品信息
//            throw new YamiShopBindException("yami.product.not.exist");
//        }
//        if (!(Objects.equals(dbProduct.getStatus(), ProdStatusEnums.NORMAL.getValue())
//                || Objects.equals(dbProduct.getStatus(), ProdStatusEnums.SHOP_OFFLINE.getValue()))) {
//            // 商品不在正常状态，修改失败
//            throw new YamiShopBindException("yami.product.on.normal");
//        }
//        Long shopId = SecurityUtils.getShopUser().getShopId();
//        ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
//        if (Objects.equals(shopDetail.getShopStatus(), ShopStatus.OFFLINE.value())) {
//            // 店铺处于违规下线中，不能修改商品，请联系管理员后重试
//            throw new YamiShopBindException("yami.product.shop.offline");
//        }
//
//        if(Objects.equals(productParam.getStatus(),1)){
//            Category category = categoryService.getById(dbProduct.getCategoryId());
//            if (Objects.equals(category.getStatus(), 0)){
//                // 平台分类处于下线中，商品不能上架，请联系管理员后再进行操作
//                throw new YamiShopBindException("yami.product.category.offline");
//            }
//            Category shopCategory = categoryService.getById(dbProduct.getShopCategoryId());
//            if (Objects.equals(shopCategory.getStatus(), 0)){
//                // 本店分类处于下线中，商品不能上架
//                throw new YamiShopBindException("yami.product.shop.category.offline");
//            }
//        }
//
//        Product product = new Product();
//        product.setProdId(prodId);
//        product.setStatus(prodStatus);
//        if (prodStatus == 1) {
//            product.setPutawayTime(new Date());
//        }
//        dbProduct.setStatus(prodStatus);
//        // 商品状态改变时的发送事件，让活动下线
//        applicationContext.publishEvent(new ProdChangeStatusEvent(dbProduct, dbProduct.getStatus()));
//
//        productService.updateById(product);
//
//        // 如果是商品下架操作
//        // 此段代码可以用Spring事件监听进行解耦，时间紧迫，以后再算，有待优化。
//        if (prodStatus == 0) {
//            // 下架时检查是否有拼团活动，有的话使之全部失效
//            LambdaQueryWrapper<GroupProd> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//            lambdaQueryWrapper
//                    .eq(GroupProd::getShopId, shopId)
//                    .eq(GroupProd::getProdId, prodId)
//                    .eq(GroupProd::getStatus, 1);
//            List<GroupProd> groupProdList = groupProdService.list(lambdaQueryWrapper);
//            for (GroupProd groupProd : groupProdList) {
//                // 查看团购活动
//                ApiGroupActivityDto apiGroupActivityInfo = groupActivityService.getApiGroupActivityInfo(groupProd.getGroupActivityId(), groupProd.getProdId());
//                // 当团购活动ID存在且处于进行中的状态时才对拼团商品进行失效处理
//                if (Objects.nonNull(apiGroupActivityInfo) && Objects.equals(apiGroupActivityInfo.getActivityStatus(), ActivityStatusEnum.UNDER_WAY.value())) {
//                    groupProdService.invalidGroupProd(groupProd);
//                }
//            }
//        }
//
//        productService.removeProdCacheByProdId(prodId,LanguageEnum.LANGUAGE_ZH_CN.getLang());
//        productService.removeProdCacheByProdId(prodId,LanguageEnum.LANGUAGE_EN.getLang());
//        List<String> userIds = basketService.listUserIdByProdId(prodId);
//
//        for (String userId : userIds) {
//            basketService.removeShopCartItemsCacheByUserId(userId,LanguageEnum.LANGUAGE_ZH_CN.getLang());
//            basketService.removeShopCartItemsCacheByUserId(userId,LanguageEnum.LANGUAGE_EN.getLang());
//        }
//        return ResponseEntity.ok().build();
//    }
//
//    /**
//     * 批量更新商品状态
//     * @param ids
//     * @param status
//     * @return
//     */
//    @PutMapping("/batchProdStatus/{status}")
//    @PreAuthorize("@pms.hasPermission('prod:prod:batchStatus')")
//    public ResponseEntity<Void> batchShopStatus(@RequestBody Long[] ids, @PathVariable("status") Integer status) {
//        // TODO 待优化
//        ProductParam product = new ProductParam();
//        product.setStatus(status);
//        StringBuilder stringBuilder = new StringBuilder();
//        for (Long prodId : ids) {
//            product.setProdId(prodId);
//            try {
//                shopStatus(product);
//            } catch (Exception e) {
//                Product dbProduct = productService.getProductByProdId(prodId, I18nMessage.getDbLang());
//                //【商品名】:错误信息xxx (【数码相机】:本店分类处于下线中，商品不能上架)
//                //stringBuffer.append("【"+dbProduct.getProdName()+"】:"+e.getMessage()+"\n");
//                stringBuilder.append("【").append(dbProduct.getProdName()).append("】  ");
//            }
//        }
//        if (stringBuilder.length()>0){
//            throw new YamiShopBindException("部分商品状态修改异常，请刷新页面后手动检查：" + stringBuilder.toString());
//        }
//        return ResponseEntity.ok().build();
//    }
//
//    /**
//     * 违规商品提交审核
//     */
//    @PostMapping("/auditApply")
//    @PreAuthorize("@pms.hasPermission('prod:prod:audit')")
//    public ResponseEntity<Void> auditApply(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
//        Product product = productService.getProductByProdId(offlineHandleEventAuditParam.getHandleId(), I18nMessage.getDbLang());
//        if (product == null) {
//            // 商品信息不存在
//            throw new YamiShopBindException("yami.product.no.exist");
//        }
//        productService.auditApply(offlineHandleEventAuditParam.getEventId(), offlineHandleEventAuditParam.getHandleId(), offlineHandleEventAuditParam.getReapplyReason());
//        // 移除缓存
//        productService.removeProdCacheByProdId(product.getProdId(),LanguageEnum.LANGUAGE_ZH_CN.getLang());
//        productService.removeProdCacheByProdId(product.getProdId(),LanguageEnum.LANGUAGE_EN.getLang());
//        return ResponseEntity.ok().build();
//    }
//
//    /**
//     * 通过prodId获取最新下线商品的事件
//     */
//    @GetMapping("/getOfflineHandleEventByProdId/{prodId}")
//    public ResponseEntity<OfflineHandleEvent> getByProdId(@PathVariable Long prodId) {
//        OfflineHandleEvent offlineHandleEvent = offlineHandleEventService.getProcessingEventByHandleTypeAndHandleId(OfflineHandleEventType.PROD.getValue(), prodId);
//        return ResponseEntity.ok(offlineHandleEvent);
//    }

    /**
     * 导出商品
     *
     * @param productParam 导出商品筛选条件
     */
    @GetMapping("/exportProd")
    @PreAuthorize("@pms.hasPermission('prod:prod:exportProd')")
    public void exportProd(ProductExportParam productParam, HttpServletResponse response) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        productParam.setShopId(shopId);
//        productParam.setStatus(productParam.getStatus() == null ?ProdStatusEnums.NORMAL.getValue():productParam.getStatus());
        productService.exportProd(productParam,response,shopId);
    }

    @GetMapping(value = "/downloadModel")
    @ResponseBody
    @PreAuthorize("@pms.hasPermission('prod:prod:downloadModel')")
    public ResponseEntity downloadModel(HttpServletResponse response) {

        productService.downloadModel(response,SecurityUtils.getShopUser().getShopId());
        // 开始下载模板
        return ResponseEntity.ok(I18nMessage.getMessage("yami.product.download"));
    }

//
//    @ApiOperation(value = "导入文件")
//    @RequestMapping(value = "/exportExcel", method = RequestMethod.POST)
//    @ResponseBody
//    public ResponseEntity exportExcel(@RequestParam("excelFile") MultipartFile excelFile) throws Exception {
//        if(excelFile == null) {
//            // 网络繁忙，请稍后重试
//            throw new YamiShopBindException("yami.network.busy");
//        }
//        Long shopId = SecurityUtils.getShopUser().getShopId();
//        return ResponseEntity.ok(productService.parseFile(excelFile,shopId));
//    }
//
//    private void checkParam(ProductParam productParam) {
//
//        Product.DeliveryModeVO deliveryMode = productParam.getDeliveryModeVo();
//        boolean hasDeliverMode = deliveryMode != null
//                && (deliveryMode.getHasShopDelivery() || deliveryMode.getHasUserPickUp() || deliveryMode.getHasCityDelivery());
//        if (!hasDeliverMode) {
//            // 请选择配送方式
//            throw new YamiShopBindException("yami.product.dvy.type");
//        }
//        Category category = categoryService.getById(productParam.getCategoryId());
//        if(category.getGrade() < 2){
//            // 请选择三级分类
//            throw new YamiShopBindException("yami.prod.category.error");
//        }
//        List<Sku> skuList = productParam.getSkuList();
//        boolean isAllUnUse = true;
//        for (Sku sku : skuList) {
//            if (sku.getStatus() == 1) {
//                isAllUnUse = false;
//            }
//        }
//        if (isAllUnUse) {
//            // 至少要启用一种商品规格
//            throw new YamiShopBindException("yami.product.enable.sku");
//        }
//        if (Objects.equals(1,productParam.getIsCompose())) {
//            // 如果是组合商品，必须又一个sku是与单品关联的
//            productParam.getSkuList().forEach(i->
//                    {
//                        if (CollectionUtils.isEmpty(i.getSkuSingleProds())) {
//                            // 组合单品必须关联一个单品
//                            throw new YamiShopBindException("yami.product.group.check");
//                        }
//                        for (RetailProdParam skuSingleProd : i.getSkuSingleProds()) {
//                            if (skuSingleProd.getRetailNums() <= 0) {
//                                throw new YamiShopBindException(i.getSkuName() + skuSingleProd.getName() + I18nMessage.getMessage("yami.retail.prod.compose"));
//                            }
//                        }
//                    }
//            );
//        }
//    }


}
